package com.zc.manager.notify.service;

import com.zc.component.notify.core.template.TemplateProvider;
import lombok.extern.slf4j.Slf4j;
import com.zc.component.notify.core.NotifyType;
import com.zc.component.notify.core.template.AbstractTemplateManager;
import com.zc.component.notify.core.template.TemplateProperties;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;

@Service
@Slf4j
public class DefaultTemplateManager extends AbstractTemplateManager implements BeanPostProcessor {

    @Resource
    private NotifyTemplateService notifyTemplateService;

    @Override
    protected Mono<TemplateProperties> getProperties(NotifyType type, String id) {
        Mono<TemplateProperties> mono =
                Mono.just(notifyTemplateService.selectNotifyTemplateById(Long.valueOf(id) ).toTemplateProperties());
        return mono;
    }
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof TemplateProvider) {
            register(((TemplateProvider) bean));
        }
        return bean;
    }

}
